package huixue.cn.com.rxapp.rx;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import huixue.cn.com.library.util.SdUtils;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.SimpleActivity;
import huixue.cn.com.rxapp.util.LogUtils;
import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

public class HelloChartActivity extends SimpleActivity {

    private int totalDays = 20;//总共有多少天的数据显示
    private float minY = 0f;//Y轴坐标最小值
    private float maxY = 100f;//Y轴坐标最大值

    String[] labelsX = new String[totalDays];//X轴的标注
    int[] valuesY = new int[totalDays];//图表的数据点

    private List<PointValue> mPointValues = new ArrayList<PointValue>();
    private List<AxisValue> mAxisXValues = new ArrayList<AxisValue>();
    private List<AxisValue> mAxisYValues = new ArrayList<AxisValue>();

    private LineChartView lineChart;

    private boolean hasAxesY = true; //是否需要Y坐标
    private String axesYName = "axesYName";//Y坐标名称
    private String axesXName = "axesXName";


    private boolean hasLines = true;//是否要折线连接
    private boolean hasPoints = true;//数据点是否要标注
    private ValueShape shape = ValueShape.CIRCLE;//数据标注点形状,这里是圆形 （有三种 ：ValueShape.SQUARE  ValueShape.CIRCLE  ValueShape.DIAMOND）
    private boolean isFilled = false;//是否需要填充和X轴之间的空间
    private boolean isCubic = false;//曲线是否平滑，即是曲线还是折线
    private boolean hasLabels = false;//数据点是否显示数据值
    private boolean hasLabelForSelected = true;//点击数据坐标提示数据（设置了这个hasLabels(true);就无效）
    private boolean hasTiltedLabels = false;  //X坐标轴字体是斜的显示还是直的，true是斜的显示
    private String lineColor = "#FF0000";//折现颜色(#FF0000红色)
    private int textColor = Color.RED;//设置字体颜色


    @Override
    public int getLayout() {
        return R.layout.activity_hello_chart;
    }

    @Override
    public void initView() {
        huixue.cn.com.library.util.LogUtils.e("ext-path:"+ SdUtils.getExternalStorageDirectory());

        lineChart = (LineChartView) findViewById(R.id.chart);
        initData();
        getAxisXYLables();//获取x轴的标注
        getAxisPoints();//获取坐标点
        initLineChart();//初始化
    }

    @Override
    public void addEvent() {

    }

    private void initData() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        SimpleDateFormat format = new SimpleDateFormat("M-d");

        labelsX[totalDays-1] = "今天";
        for (int i=1; i< totalDays; i++) {
            cal.add(Calendar.DATE, -1);
            String str = format.format(cal.getTime());
            LogUtils.e("initData: ", str);
            labelsX[totalDays - i - 1] = str;
        }

        for (int j = 0; j < totalDays; j++) {
            Random random = new Random();
            valuesY[j] = random.nextInt(10) + 45;
        }
    }

    private void getAxisXYLables() {
        for (int i = 0; i < labelsX.length; i++) {
            mAxisXValues.add(new AxisValue(i).setLabel(labelsX[i]));
        }
        for (float i = minY; i <= maxY; i+=10) {
            mAxisYValues.add(new AxisValue(i).setLabel(i+""));
        }
    }

    private void getAxisPoints() {
        for (int i = 0; i < valuesY.length; i++) {
            mPointValues.add(new PointValue(i, valuesY[i]));
        }
    }

    private void initLineChart() {
        Line line = new Line(mPointValues).setColor(Color.parseColor(lineColor));  //折线的颜色
        List<Line> lines = new ArrayList<Line>();
        line.setShape(shape);//折线图上每个数据点的形状
        line.setCubic(isCubic);//曲线是否平滑，即是曲线还是折线
        line.setFilled(isFilled);//是否填充曲线的面积
        line.setHasLabels(hasLabels);//曲线的数据坐标是否加上备注
        line.setHasLabelsOnlyForSelected(hasLabelForSelected);//点击数据坐标提示数据
        line.setHasLines(hasLines);//是否用线显示。如果为false 则没有曲线只有点显示
        line.setHasPoints(hasPoints);//是否显示圆点
        lines.add(line);
        LineChartData data = new LineChartData();
        data.setLines(lines);

        //坐标轴X
        Axis axisX = new Axis(); //X轴
        axisX.setHasTiltedLabels(hasTiltedLabels);  //X坐标轴字体是斜的显示还是直的，true是斜的显示
        axisX.setTextColor(textColor);  //设置字体颜色
        axisX.setName(axesXName);
        axisX.setMaxLabelChars(0);
        axisX.setValues(mAxisXValues);
        data.setAxisXBottom(axisX);//x 轴在底部

        //坐标轴Y
        if (hasAxesY) {
            Axis axisY = new Axis();
            axisY.setHasLines(false);
            axisY.setName(axesYName);
            axisY.setValues(mAxisYValues);
            data.setAxisYLeft(axisY);
        }

        //设置行为属性，支持缩放、滑动以及平移
        lineChart.setInteractive(true);
        lineChart.setZoomType(ZoomType.HORIZONTAL);
        lineChart.setMaxZoom((float) 4);//最大方法比例
        lineChart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
        lineChart.setLineChartData(data);
        lineChart.setVisibility(View.VISIBLE);

        Viewport v = new Viewport(lineChart.getMaximumViewport());
        v.bottom = minY;
        v.top = maxY;
        //固定Y轴的范围,如果没有这个,Y轴的范围会根据数据的最大值和最小值决定,这不是我想要的
        lineChart.setMaximumViewport(v);

        //这2个属性的设置一定要在lineChart.setMaximumViewport(v);这个方法之后,不然显示的坐标数据是不能左右滑动查看更多数据的
        v.left = totalDays - 7;
        v.right = totalDays - 1;
        lineChart.setCurrentViewport(v);
    }
}
